﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.Collections;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint;
using System.Web;

namespace LanguageConfigurationSolution.LanguageSwitch
{
    public abstract class LanguageSwitchBase : WebControl, INamingContainer
    {
        [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(LanguageSwitchBase)), TemplateInstance(TemplateInstance.Single)]
        public ITemplate HeaderTemplate
        {
            get;
            set;
        }

        [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(LanguageSwitchBase)), TemplateInstance(TemplateInstance.Single)]
        public ITemplate FooterTemplate
        {
            get;
            set;
        }

        [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(LanguageSwitchContainer)), TemplateInstance(TemplateInstance.Multiple)]
        public ITemplate LanguageTemplate
        {
            get;
            set;
        }

        [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(LanguageSwitchContainer)), TemplateInstance(TemplateInstance.Single)]
        public ITemplate CurrentTemplate
        {
            get;
            set;
        }

        [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(LanguageSwitchBase)), TemplateInstance(TemplateInstance.Multiple)]
        public ITemplate SeperatorTemplate
        {
            get;
            set;
        }

        [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(LanguageSwitchBase)), TemplateInstance(TemplateInstance.Single)]
        public ITemplate EmptyTemplate
        {
            get;
            set;
        }

        public string HeaderTemplateUrl
        {
            get;
            set;
        }

        public string FooterTemplateUrl
        {
            get;
            set;
        }

        public string LanguageTemplateUrl
        {
            get;
            set;
        }

        public string CurrentTemplateUrl
        {
            get;
            set;
        }

        public string SeperatorTemplateUrl
        {
            get;
            set;
        }

        public string EmptyTemplateUrl
        {
            get;
            set;
        }

        private ArrayList _Suppress = new ArrayList();

        [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty)]
        public ArrayList Suppress
        {
            get
            {
                return _Suppress;
            }
        }

        protected abstract void CreateLanguageSwitches();

        protected override void CreateChildControls()
        {
            if (!string.IsNullOrEmpty(HeaderTemplateUrl)) HeaderTemplate = Page.LoadTemplate(HeaderTemplateUrl);
            if (!string.IsNullOrEmpty(FooterTemplateUrl)) FooterTemplate = Page.LoadTemplate(FooterTemplateUrl);
            if (!string.IsNullOrEmpty(EmptyTemplateUrl)) EmptyTemplate = Page.LoadTemplate(EmptyTemplateUrl);
            if (!string.IsNullOrEmpty(SeperatorTemplateUrl)) SeperatorTemplate = Page.LoadTemplate(SeperatorTemplateUrl);
            if (!string.IsNullOrEmpty(LanguageTemplateUrl)) LanguageTemplate = Page.LoadTemplate(LanguageTemplateUrl);
            if (!string.IsNullOrEmpty(CurrentTemplateUrl)) CurrentTemplate = Page.LoadTemplate(CurrentTemplateUrl);

            CreateLanguageSwitches();
            base.CreateChildControls();
        }

        public string StyleLibraryUrl
        {
            get
            {
                string url = SPUrlUtility.CombineUrl(SPContext.Current.Site.ServerRelativeUrl, "/Style Library/");
                return url;
            }
        }


        public virtual void JumpLanguage(object sender, EventArgs e)
        {
            Control parent = sender as Control;
            while (parent != null)
            {
                if (parent is LanguageSwitchContainer)
                {
                    LanguageSwitchContainer lscontainer = (LanguageSwitchContainer)parent;
                    HttpCookie cookie = Page.Request.Cookies[RootLandingPage.DEFAULTSUBWEBCOOKIENAME];
                    if (cookie == null)
                    {
                        cookie = new HttpCookie(RootLandingPage.DEFAULTSUBWEBCOOKIENAME);
                    }
                    cookie.Value = lscontainer.RootWeb.Name;
                    cookie.Expires = DateTime.Now.AddMonths(1);
                    Page.Response.Cookies.Add(cookie);

                    Page.Response.Redirect(lscontainer.RootWeb.ServerRelativeUrl);
                }
                parent = parent.Parent;
            }
        }
    }
}
